package com.gxdf.uc;

import java.util.ArrayList;
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.google.gson.stream.JsonReader;
import com.gxdf.uc.data.Field;
import com.gxdf.uc.data.FieldOld;
import com.gxdf.uc.data.FieldType;
import com.gxdf.uc.data.Form;
import com.gxdf.uc.data.GxdfConcept;
import com.gxdf.uc.data.GxdfEditText;
import com.gxdf.uc.data.GxdfField;
import com.gxdf.uc.data.GxdfForm;
import com.gxdf.uc.data.GxdfGroupView;
import com.gxdf.uc.data.GxdfSpinner;
import com.gxdf.uc.data.IGxdfItem;
import com.gxdf.uc.data.Propiedad;
import com.gxdf.uc.data.Propiedades;
import com.gxdf.uc.data.RenderingType;
import com.gxdf.uc.data.TypeDomain;

/**
 * 
 * @author Diego Busca
 * Parsea el JSON e instancia un GxdfForm
 *
 */
public class JsonToForm {

	public JsonToForm() {		
	}
	
	//TODO: Solamente guardar los gxfields en el Gxdform
	public static Form getForm(String jform){
		
		Form form = new Form();
		
		
		try {			
			
			JSONObject fo= new JSONObject(jform);
			//JSONObject obj = fo.getJSONObject("FormInstanceSDT");
			//JSONObject jfieldobj = obj.getJSONObject("Fields");
			JSONArray  jfields = fo.getJSONArray("Fields");
			
			//Log.i("Fields array length", String.valueOf(jfieldb.length()));	

			
			for(int i=0;i<jfields.length();i++){
				
				Field f =new Field();
				
				JSONObject jfield = jfields.getJSONObject(i);
				JSONObject jfieldType = jfield.getJSONObject("Type");
				JSONObject jconcept = jfield.getJSONObject("Concept");
				JSONObject jvalues = jfield.getJSONObject("Values");
				
				
				FieldType type = new FieldType();
				type.setName(jfieldType.getString("Name"));
				type.setPreloaded((jfieldType.getString("PreloadedDomain").equals("Y")) ? true : false);
				
				JSONObject jpValuesobj = jfieldType.getJSONObject("PossibleValues");
				JSONArray pValues = jpValuesobj.getJSONArray("Value");
				
				
				//Possible Values
				ArrayList<String> posValues = new ArrayList<String>();	
				
				
				for (int y=0; y<pValues.length();y++){					
					String v = (String) pValues.get(y);
					posValues.add(v);
				}
				type.setPosValues(posValues);
				
				
				//FIELD ATTRS
				//--------------------------------------------------------------------------------					
				
				f.setName(jfield.getString("Name"));
				f.setLabelValue(jfield.getString("Label"));	
				f.setRequired( (jfield.getString("Required").equals("Y") ? true : false));
				f.setFormatValidation(jfield.getString("FormatValidation"));
				//f.setValue(jfield.getString("Value"));
				f.setCollection((jfield.getString("IsCollection").equals("Y") ? true : false));
				f.setFormatValidation((jfield.getString("FormatValidation")));
				f.setLength(Integer.parseInt(jfield.getString("Length")));
				f.setConcepto(jconcept.getString("ConceptName"));
				
							

				
				//Values
				ArrayList<String> values = new ArrayList<String>();
				JSONArray arValues = jvalues.getJSONArray("Value");
				for (int j=0;j<arValues.length();j++){
					
					JSONObject v = arValues.getJSONObject(j);
					values.add(v.getString("Value"));
				}
				
				f.setValues(values);
				

				
				//FIELD TYPE
				//--------------------------------------------------------------------------------
				
				if(jfieldType.getString("TypeDomain").equals("Literal")){
					type.setTypeDomain(TypeDomain.LITERAL);
				}
				else if(jfieldType.getString("TypeDomain").equals("Number")) {
					type.setTypeDomain(TypeDomain.NUMBER);
				}				
				else if(jfieldType.getString("TypeDomain").equals("Date")) {
					type.setTypeDomain(TypeDomain.DATE);
				}
				else if(jfieldType.getString("TypeDomain").equals("Email")) {
					type.setTypeDomain(TypeDomain.EMAIL);
				}
				
				f.setType(type);
				
							
				//RENDERING TYPE
				//--------------------------------------------------------------------------------
				if(jfieldType.getString("Rendering").equals("TextBox") && jfield.getString("IsCollection").equals("N")){
					type.setRender(RenderingType.TEXTBOX);
					//f =  new GxdfEditText(context));			
				}
				else if(jfieldType.getString("Rendering").equals("TextBox") && jfield.getString("IsCollection").equals("Y")){
					type.setRender(RenderingType.TEXTBOX_LIST);								
					//f =  new GxdfGroupView(context, jfield.getString("Name"), jfield.getString("Label"));			
				}
				else if(jfieldType.getString("Rendering").equals("Combo")){
					type.setRender(RenderingType.COMBO);
					//f = new GxdfSpinner(context, jfield.getString("Name"), jfield.getString("Label"), jfield.getString("Value"), options)
				}
				else if(jfieldType.getString("Rendering").equals("Calendar")){
					type.setRender(RenderingType.CALENDAR);
				}	
				
				f.setType(type);
				
				//ADD FORM FIELDS
				//-------------------------------------------------------------------------------
				form.addField(f);
				
			}
			
					
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return form;
	}
	
	//private void renderField(Field f)

}
